package com.shanling.shanlingcontroller.ui.activity;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.actions.ibluz.manager.BluzManager;
import com.actions.ibluz.manager.BluzManagerData;
import com.chipsguide.lib.bluetooth.commands.BluetoothDeviceCommandManager;
import com.db.chart.model.LineSet;
import com.db.chart.view.AxisController;
import com.db.chart.view.ChartView;
import com.db.chart.view.LineChartView;
import com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBar;
import com.linkplay.lpvr.avslib.LPAVSManager;
import com.linkplay.lpvr.blelib.manager.LPAVSBTManager;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EQBean;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.utils.EQBeansUtils;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.view.EQPopupWindow;
import com.shanling.shanlingcontroller.view.NoChooseDialog;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EQActivity extends BaseAppCompatActivity implements EQPopupWindow.ItemOnClickInterface, SeekBar.OnSeekBarChangeListener, BluzManagerData.OnCustomCommandListener {

    @BindView(R.id.lineChart)
    LineChartView chart;
    @BindView(R.id.tv_eq1)
    TextView tvEq1;
    @BindView(R.id.tv_eq2)
    TextView tvEq2;
    @BindView(R.id.tv_eq3)
    TextView tvEq3;
    @BindView(R.id.tv_eq4)
    TextView tvEq4;
    @BindView(R.id.tv_eq5)
    TextView tvEq5;
    @BindView(R.id.tv_eq6)
    TextView tvEq6;
    @BindView(R.id.tv_eq7)
    TextView tvEq7;
    @BindView(R.id.tv_eq8)
    TextView tvEq8;
    @BindView(R.id.tv_eq9)
    TextView tvEq9;
    @BindView(R.id.tv_eq10)
    TextView tvEq10;
    @BindView(R.id.rl_eq)
    RelativeLayout rlEq;
    @BindView(R.id.tv_Preset)
    TextView tvPreset;
    @BindView(R.id.tv_Reset)
    TextView tvReset;
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.seekBar1)
    VerticalSeekBar seekBar1;
    @BindView(R.id.seekBar2)
    VerticalSeekBar seekBar2;
    @BindView(R.id.seekBar3)
    VerticalSeekBar seekBar3;
    @BindView(R.id.seekBar4)
    VerticalSeekBar seekBar4;
    @BindView(R.id.seekBar5)
    VerticalSeekBar seekBar5;
    @BindView(R.id.seekBar6)
    VerticalSeekBar seekBar6;
    @BindView(R.id.seekBar7)
    VerticalSeekBar seekBar7;
    @BindView(R.id.seekBar8)
    VerticalSeekBar seekBar8;
    @BindView(R.id.seekBar9)
    VerticalSeekBar seekBar9;
    @BindView(R.id.seekBar10)
    VerticalSeekBar seekBar10;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    private String data[] = {"31", "62", "125", "250", "500", "1K", "2K", "4K", "8K", "16k"};
    private byte bond = 1;
    private NoChooseDialog noChooseDialog;
    private EQPopupWindow eqPopupWindow;
    private List<EQBean> eqBeans;
    private byte mode = 0;
    LineSet dataset;
    Paint paint;
    float[] points;
    private LPAVSBTManager lpavsbtManager;



    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_eq;
    }

    @Override
    protected void initViewsAndEvents() {

        noChooseDialog = new NoChooseDialog(this, R.style.myDialog, R.string.Equalizer, R.string.dialog_incorrect_state_message, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                noChooseDialog.dismiss();
            }
        });
        eqBeans = EQBeansUtils.getEQbeans(this,false);
        eqPopupWindow = new EQPopupWindow(this);
        eqPopupWindow.setItemOnClickInterface(this);
        getInformationFromDevice();
        setPointAndListener();
    }

    private void setPointAndListener() {
        paint = new Paint();
        dataset = new LineSet();
        points = new float[10];
        for (int i = 0; i < 10; i++) {
            points[i] = 12;
            dataset.addPoint(data[i], points[i]);
        }
        paint.setColor(Color.parseColor("#CAB487"));
        paint.setStrokeWidth((float) (1.10 * 1.0));
        dataset.setColor(Color.parseColor("#CAB487"));
        dataset.setSmooth(true);
        dataset.setThickness(2);
        chart.setXAxis(false);
        chart.setYAxis(false);
        chart.setYLabels(AxisController.LabelPosition.NONE);
        chart.setXLabels(AxisController.LabelPosition.NONE);
        chart.setGrid(ChartView.GridType.NONE, 7, 10, paint);
        chart.setAxisBorderValues(0, 24);
        chart.addData(dataset);
        chart.show();
        seekBar1.setOnSeekBarChangeListener(this);
        seekBar2.setOnSeekBarChangeListener(this);
        seekBar3.setOnSeekBarChangeListener(this);
        seekBar4.setOnSeekBarChangeListener(this);
        seekBar5.setOnSeekBarChangeListener(this);
        seekBar6.setOnSeekBarChangeListener(this);
        seekBar7.setOnSeekBarChangeListener(this);
        seekBar8.setOnSeekBarChangeListener(this);
        seekBar9.setOnSeekBarChangeListener(this);
        seekBar10.setOnSeekBarChangeListener(this);
    }

    private void getInformationFromDevice() {
        if (preferenceUtil.getBleType() == 2) {
            BluzManager bluzManager = customApplication.getBluetoothDeviceManager().getBluzManager();
            bluzManager.setOnCustomCommandListener(this);
            BluetoothDeviceCommandManager.c(new byte[]{13, 7, 5, 2, 22, 0, 14});
        } else {
            lpavsbtManager= LPAVSManager.getInstance(this).getBtManager();
            lpavsbtManager.BLEPass(new byte[]{0x40, (byte)0x80, 0x01, 0x00, 0x00});
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case Constants.BLUETOOTH_STATE:
                BluetoothState state = (BluetoothState) eventCenter.getData();
                if (state == null) {
                    return;
                }
                switch (state.getState()) {
                    case Constants.DISCONNECTED:
                        ToastUtils.showLongToast(customApplication, R.string.bluetooth_disconnected);
                        finish();
                        break;
                }
                break;
            case  Constants.EQ_DATA:
               byte[]data= (byte[]) eventCenter.getData();

               if(data.length==21){
                   mode=data[20];
                   setMode(mode);
                   setProgress(data);
               }
                break;
        }

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    @OnClick({R.id.tv_Preset, R.id.tv_Reset,R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_Preset:
                eqPopupWindow.show(eqBeans, tvPreset);
                break;
            case R.id.tv_Reset:
                seekBar1.setProgress(120);
                seekBar2.setProgress(120);
                seekBar3.setProgress(120);
                seekBar4.setProgress(120);
                seekBar5.setProgress(120);
                seekBar6.setProgress(120);
                seekBar7.setProgress(120);
                seekBar8.setProgress(120);
                seekBar9.setProgress(120);
                seekBar10.setProgress(120);
                if(preferenceUtil.getBleType()==2){
                    BluetoothDeviceCommandManager.c(new byte[]{13, 17, 5, 3,23,0,0,0,0,0,0,0,0,0,0,7, 14});
                }else{
                    lpavsbtManager.BLEPass(new byte[]{0x42, (byte) 0x80,0x0b,0x00,0,0,0,0,0,0,0,0,0,0,7});
                }
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        eqPopupWindow.dissmiss();
    }

    @Override
    public void onItemClick(View view, int position) {
        EQBean eqBean = eqBeans.get(position);
        int[] eqDate = eqBean.getEqDate();
        if(position!=0){
            eqBeans.get(0).setEqDate(eqDate);
        }
        switch (position){
            case 0:
                mode=7;
                break;
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                mode= (byte) position;
                break;
        }
        seekBar1.setProgress(eqDate[0]);
        seekBar2.setProgress(eqDate[1]);
        seekBar3.setProgress( eqDate[2]);
        seekBar4.setProgress(eqDate[3]);
        seekBar5.setProgress( eqDate[4]);
        seekBar6.setProgress( eqDate[5]);
        seekBar7.setProgress( eqDate[6]);
        seekBar8.setProgress( eqDate[7]);
        seekBar9.setProgress( eqDate[8]);
        seekBar10.setProgress( eqDate[9]);
        if(preferenceUtil.getBleType()==2){
            BluetoothDeviceCommandManager.c(new byte[]{13, 17, 5, 3,23,(byte) (eqDate[0]-120),
                    (byte) (eqDate[1]-120),(byte)(eqDate[2]-120),(byte)(eqDate[3]-120),
                    (byte) (eqDate[4]-120),(byte) (eqDate[5]-120),(byte) (eqDate[6]-120),
                    (byte)(eqDate[7]-120),(byte) (eqDate[8]-120),
                    (byte)(eqDate[9]-120),mode, 14});
        }else {
            lpavsbtManager.BLEPass(new byte[]{0x42, (byte) 0x80,0x0b,0x00,(byte)(eqDate[0]-120),
                    (byte) (eqDate[1]-120),(byte)(eqDate[2]-120),(byte)(eqDate[3]-120),
                    (byte) (eqDate[4]-120),(byte) (eqDate[5]-120),(byte) (eqDate[6]-120),
                    (byte)(eqDate[7]-120),(byte) (eqDate[8]-120),
                    (byte)(eqDate[9]-120),mode});
        }
    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        int id = seekBar.getId();
        float value =(progress - 120)/(float)10;
        float point=progress/10;
        switch (id) {
            case R.id.seekBar1:
                points[0] = point;
                setValue(value,tvEq1);
                break;
            case R.id.seekBar2:
                points[1] = point;
                setValue(value,tvEq2);
                break;
            case R.id.seekBar3:
                points[2] = point;
                setValue(value,tvEq3);
                break;
            case R.id.seekBar4:
                points[3] = point;
                setValue(value,tvEq4);
                break;
            case R.id.seekBar5:
                points[4] = point;
                setValue(value,tvEq5);
                break;
            case R.id.seekBar6:
                points[5] = point;
                setValue(value,tvEq6);
                break;
            case R.id.seekBar7:
                points[6] = point;
                setValue(value,tvEq7);
                break;
            case R.id.seekBar8:
                points[7] = point;
                setValue(value,tvEq8);
                break;
            case R.id.seekBar9:
                points[8] = point;
                setValue(value,tvEq9);
                break;
            case R.id.seekBar10:
                points[9] = point;
                setValue(value,tvEq10);
                break;
        }
        dataset.updateValues(points);
        chart.notifyDataUpdate();
    }
    private void  setValue(float value,TextView textView){
        textView.setText(value+"");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        if(mode!=7){
            for (int i = 0; i <eqBeans.size() ; i++) {
                eqBeans.get(i).setCheck(false);
            }
            eqBeans.get(0).setCheck(true);
            mode=7;
        }
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        int id = seekBar.getId();
        int progress=seekBar.getProgress();
        byte value= (byte)(progress-120);
        switch (id) {
            case R.id.seekBar1:
                bond=10;
                break;
            case R.id.seekBar2:
                bond=11;
                break;
            case R.id.seekBar3:
                bond=12;
                break;
            case R.id.seekBar4:
                bond=13;
                break;
            case R.id.seekBar5:
                bond=14;
                break;
            case R.id.seekBar6:
                bond=15;
                break;
            case R.id.seekBar7:
                bond=16;
                break;
            case R.id.seekBar8:
                bond=17;
                break;
            case R.id.seekBar9:
                bond=18;
                break;
            case R.id.seekBar10:
                bond=19;
                break;
        }
        if(preferenceUtil.getBleType()==2){
            BluetoothDeviceCommandManager.c(new byte[]{13, 9, 5, 3,22,bond,value,mode, 14});
        }else{
            lpavsbtManager.BLEPass(new byte[]{0x41, (byte)0x80,0x03,0x00,bond,value,mode });
        }
    }

    @Override
    public void onReady(int i, int i1, int i2, byte[] bytes) {
        if(bytes.length==17&&bytes[3]==2){
            mode=bytes[15];
            Log.e("weiyu","模式"+mode);
            setMode(mode);
            setProgress(bytes);
        }
    }
    private void setProgress(byte[] bytes) {
        if(preferenceUtil.getBleType()==2){
            seekBar1.setProgress(bytes[5]+120);
            seekBar2.setProgress(bytes[6]+120);
            seekBar3.setProgress(bytes[7]+120);
            seekBar4.setProgress(bytes[8]+120);
            seekBar5.setProgress(bytes[10]+120);
            seekBar6.setProgress(bytes[11]+120);
            seekBar7.setProgress(bytes[12]+120);
            seekBar8.setProgress(bytes[13]+120);
            seekBar9.setProgress(bytes[14]+120);
            seekBar10.setProgress(bytes[15]+120);
        }else {
            seekBar1.setProgress(bytes[10]+120);
            seekBar2.setProgress(bytes[11]+120);
            seekBar3.setProgress(bytes[12]+120);
            seekBar4.setProgress(bytes[13]+120);
            seekBar5.setProgress(bytes[14]+120);
            seekBar6.setProgress(bytes[15]+120);
            seekBar7.setProgress(bytes[16]+120);
            seekBar8.setProgress(bytes[17]+120);
            seekBar9.setProgress(bytes[18]+120);
            seekBar10.setProgress(bytes[19]+120);
        }
    }
    private void setMode(int mode) {
        switch (mode){
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
                eqBeans.get(mode).setCheck(true);
                break;
            case 7:
                eqBeans.get(0).setCheck(true);
                break;
        }
    }
}
